﻿using System;
using System.Data;
using System.Data.SqlClient;
using JobSiteStarterKit.DAL;

namespace JobSiteStarterKit.BOL
{
    /// <summary>
    /// Summary description for Jobseeker
    /// </summary>
    public class Employer
    {
       
        public struct EmployerData
        {
            public int CompanyId;
            public  string UserName;
            public  string Password;
            public  string EmailId;
            public  string CompanyName;
            public  string ContactPerson;
            public  string CompanyType;
            public  string IndustryType;
            public  string Address;
            public  string City;
            public  int State;
            public  int Country;
            public  string Pincode;
            public  string Phonenumber;
            public DateTime RegistrationDate;
            public DateTime ExpiryDate;
            public DateTime EffectiveDate;
            public int Status;
            public string Profile;
            public string WebSite;
            
        }
        
        EmployerData data;

        public Employer()
        {
            //
            // TODO: Add constructor logic here
            //
            
            data = new EmployerData();
        }

        public EmployerData EmployerDetails
        {
            get
            {
                return data;
            }
            set
            {
                this.data = value;
            }
        }
        public EmployerData GetEmployerData(string username)
        {
            using (DBAccess db = new DBAccess())
            {
                string prmName = "JobsDb_Companies_SelectByUserName";
                db.CommandText = prmName;
                db.AddParameter("@sUserName",username);
                SqlDataReader reader = (SqlDataReader) db.ExecuteReader();
                while (reader.Read())
                {
                    data.CompanyName = reader["CompanyName"].ToString();
                    data.CompanyType = reader["CompanyType"].ToString();
                    data.ContactPerson = reader["ContactPerson"].ToString();
                    data.Country = Convert.ToInt16(reader["CountryID"].ToString());
                    
                    data.EmailId = reader["CompanyEmail"].ToString();
                    data.City = reader["City"].ToString();
                    data.Address = reader["Address1"].ToString();
                   //data.ExpiryDate = reader[""].ToString();
                   data.IndustryType = reader["IndustryType"].ToString();
                   //data.Password = reader[""].ToString();
                   data.Phonenumber = reader["Phone"].ToString();
                   data.Pincode = reader["Zip"].ToString();
                    if(reader["RegistrationDate"].ToString() != "")
                        data.RegistrationDate = Convert.ToDateTime(reader["RegistrationDate"].ToString());
                    if (reader["EffectiveDate"].ToString() != "")
                        data.EffectiveDate = Convert.ToDateTime(reader["EffectiveDate"].ToString());
                    if (reader["ExpirationDate"].ToString() != "")
                        data.ExpiryDate = Convert.ToDateTime(reader["ExpirationDate"].ToString());
                   data.State = Convert.ToInt16(reader["StateID"].ToString());
                   data.WebSite = reader["WebSiteUrl"].ToString();
                   data.IndustryType = reader["IndustryType"].ToString();
                   data.UserName = reader["UserName"].ToString();
                   data.Profile = reader["CompanyProfile"].ToString();
                   
                }
                reader.Close();
                return data;
            }
        }
        public bool UpdateEmployer(string username)
        {
            string Proc_Name = "JobsDb_Companies_Update_1";
            try
            {
                using (DBAccess db = new DBAccess())
                {
                    db.CommandText = Proc_Name;
                    SqlParameterCollection col = (SqlParameterCollection)db.Parameters;
                    SqlParameter prmComapnyName = new SqlParameter("@CompanyName",SqlDbType.VarChar,255);
                    SqlParameter prmComapnyType = new SqlParameter("@CompanyType",SqlDbType.VarChar,50);
                    SqlParameter prmCity = new SqlParameter("@City", SqlDbType.VarChar, 50);
                    SqlParameter prmContactPerson = new SqlParameter("@ContactPerson", SqlDbType.VarChar, 100);
                    SqlParameter prmAddress = new SqlParameter("@Address1", SqlDbType.VarChar, 255);
                    SqlParameter prmZip = new SqlParameter("@Zip", SqlDbType.VarChar, 50);
                    //SqlParameter prmMobile = new SqlParameter("@Mobile", SqlDbType.VarChar, 50);
                    SqlParameter prmPhone = new SqlParameter("@Phone", SqlDbType.VarChar, 50);
                    
                    SqlParameter prmWebsite = new SqlParameter("@WebSiteUrl", SqlDbType.VarChar, 255);
                    SqlParameter prmIndustryType = new SqlParameter("@IndustryType", SqlDbType.Int);
                    SqlParameter prmCountry = new SqlParameter("@CountryID", SqlDbType.Int);
                    SqlParameter prmState = new SqlParameter("@StateID", SqlDbType.Int);
                    SqlParameter prmProfile = new SqlParameter("@CompanyProfile", SqlDbType.Text,0);
                    SqlParameter prmUserName = new SqlParameter("@UserName", SqlDbType.VarChar,50);
                    
                    //store value
                    prmComapnyName.Value = data.CompanyName;
                    prmAddress.Value = data.Address;
                    prmCity.Value = data.City;
                    prmComapnyType.Value = data.CompanyType;
                    prmUserName.Value = username;
                    prmContactPerson.Value = data.ContactPerson;
                    prmCountry.Value = data.Country;
                   
                    prmIndustryType.Value = data.IndustryType;
                    prmPhone.Value = data.Phonenumber;
                    prmProfile.Value = data.Profile;
                    prmState.Value = data.State;
                    prmWebsite.Value = data.WebSite;
                    prmZip.Value = data.Pincode;

                    col.AddRange(new SqlParameter[] {prmAddress,prmCity,prmComapnyName,
                        prmComapnyType,prmContactPerson,prmCountry,
                        prmIndustryType,prmPhone,prmProfile,prmState,prmUserName,prmWebsite,prmZip});
                    if (db.ExecuteNonQuery() > 0)
                        return true;
                    else
                        return false;
   
                }
            }
            catch
            {
                return false;
            }
            return true;
        }
            
        public bool RegisterEmployer()
        {
            string ProcName = "JobsDb_Companies_Insert";
            using (DBAccess db = new DBAccess())
                {
                    try
                    {
                        db.CommandText = ProcName;
                        SqlParameterCollection col = (SqlParameterCollection) db.Parameters;
                        
                        SqlParameter address1 = new SqlParameter("@Address1", SqlDbType.VarChar, 255);
                        address1.Value = data.Address;
                        
                        SqlParameter city = new SqlParameter("@City", SqlDbType.VarChar, 50);
                        city.Value = data.City;
                       
                        SqlParameter companyname = new SqlParameter("@CompanyName", SqlDbType.VarChar, 255);
                        companyname.Value = data.CompanyName;
                       
                        SqlParameter CompanyType = new SqlParameter("@CompanyType", SqlDbType.VarChar, 50);
                        CompanyType.Value = data.CompanyType;
                        
                        SqlParameter ContactPerson = new SqlParameter("@ContactPerson", SqlDbType.VarChar, 100);
                        ContactPerson.Value = data.ContactPerson;
                        
                        SqlParameter Country = new SqlParameter("@CountryID", SqlDbType.Int, 0);
                        Country.Value = (int) data.Country;
                       
                        //SqlParameter EffectiveDate = new SqlParameter("@EffectiveDate", SqlDbType.SmallDateTime, 0);
                        //EffectiveDate.Value = data.EffectiveDate;
                        
                        SqlParameter IndustryType = new SqlParameter("@IndustryType", SqlDbType.Int, 0);
                        //IndustryType.Value = data.EffectiveDate;
                       
                        SqlParameter Phone = new SqlParameter("@Phone", SqlDbType.VarChar, 50);
                        Phone.Value = data.Phonenumber;
                       
                        SqlParameter Zip = new SqlParameter("@Zip", SqlDbType.VarChar, 50);
                        Zip.Value = data.Pincode;
                       
                        SqlParameter RegistrationDate = new SqlParameter("@RegistrationDate", SqlDbType.SmallDateTime, 0);
                        RegistrationDate.Value = data.RegistrationDate;
                       
                        SqlParameter StateID = new SqlParameter("@StateID", SqlDbType.Int, 0);
                        StateID.Value = data.State;
                       
                        SqlParameter Status = new SqlParameter("@Status", SqlDbType.Int, 0);
                        Status.Value = data.Status;
                       
                        SqlParameter UserName = new SqlParameter("@UserName", SqlDbType.VarChar, 50);
                        UserName.Value = data.UserName;

                        SqlParameter Profile = new SqlParameter("@CompanyProfile", SqlDbType.Text, 0);
                        Profile.Value = data.Profile;

                        SqlParameter WebSite = new SqlParameter("@WebSiteUrl", SqlDbType.NVarChar, 50);
                        WebSite.Value = data.WebSite;
                        col.AddRange(new SqlParameter[] {address1, city, companyname, CompanyType,ContactPerson, Country,IndustryType, Phone,Zip, RegistrationDate,StateID, Status,UserName,Profile,WebSite});

                        db.ExecuteNonQuery();
                      

                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                   
                   
                }
                return true;
                
            }

        

        
    }
}